package org.iridium.tools.extractip;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

public class Parser {

	public static void main(String[] args) {
		Parser parser = new Parser();
		String[] src = new String[] { "ip/chinanet.txt", "ip/cmcc.txt",
				"ip/cnc.txt", "ip/crtc.txt" };

		for (String filePath : src) {
			// 载入文件
			File cmcc = new File(filePath);
			// System.out.println(cmcc.exists());
			List<IP> ipList = parser.parse(cmcc);

			String[] provinces = Constant.PROVINCES;
			// List<IP> ipListGuangDong = parser.filter(ipList, area);

			List<IP> ipListChina = parser.filter(ipList, provinces);

			parser.output(ipListChina, Constant.PARSER_TYPE_TABLE);
		}

	}

	private List<IP> filter(List<IP> ipList, String[] provinces) {

		List<IP> ipListFiltered = new ArrayList<IP>();

		for (String province : provinces) {
			for (IP ip : ipList) {
				if (ip.getDescr().toLowerCase().contains(province)
						|| ip.getNetname().toLowerCase().contains(province)) {
					ip.setProvince(province);
					ipListFiltered.add(ip);
				}
			}
		}

		return ipListFiltered;

	}

	private void output(List<IP> ipListFiltered, String parserType) {
		String outputFileName = null;
		StringBuffer sb = new StringBuffer("<table border='1'>");
		if (Constant.PARSER_TYPE_TABLE.equals(parserType)) {
			outputFileName = "ip/output" + Math.random() + ".html";
			for (IP ip : ipListFiltered) {
				String[] startAndEnd = ip.getInetnum().split("-");
				sb.append("<tr>");
				sb.append("<td>");
				sb.append(ip.getProvince());
				sb.append("</td>");
				sb.append("<td>");
				sb.append(startAndEnd[0]);
				sb.append("</td>");
				sb.append("<td>");
				sb.append(startAndEnd[1]);
				sb.append("</td>");
				sb.append("<td>");
				sb.append(ip.getDescr());
				sb.append("</td>");
				sb.append("</tr>");
			}
			sb.append("</table>");
		} else if (Constant.PARSER_TYPE_PLAIN_TEXT.equals(parserType)) {
			outputFileName = "ip/output" + Math.random() + ".txt";
			for (IP ip : ipListFiltered) {
				sb.append(ip.getInetnum()).append(", ").append(ip.getDescr())
						.append("\n");
			}
		}

		File output = new File(outputFileName);
		try {
			if (!output.exists())
				output.createNewFile();
			OutputStream out = new FileOutputStream(output);
			out.write(sb.toString().getBytes("utf-8"));
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	private List<IP> filter(List<IP> ipList, String filter) {
		List<IP> ipListFiltered = new ArrayList<IP>();
		for (IP ip : ipList) {
			if (ip.getDescr().toLowerCase().contains("guangdong")
					|| ip.getNetname().toLowerCase().contains("guangdong")) {
				ipListFiltered.add(ip);
			}
		}
		return ipListFiltered;
	}

	List<IP> parse(File file) {
		FileReader reader = null;
		BufferedReader br = null;
		// StringBuffer sb = null;
		List<IP> ipList = new ArrayList<IP>();
		try {
			IP ip = new IP();
			reader = new FileReader(file);
			br = new BufferedReader(reader);
			String line = null;
			// sb = new StringBuffer();
			while ((line = br.readLine()) != null) {
				if (line == null || line.equals("")) {
					continue;
				}
				if (line.startsWith("inetnum")) {
					ip = new IP();
					ipList.add(ip);
					String[] keyValue = line.split(":\\s");
					ip.setInetnum(keyValue[1].trim());
				}
				if (line.startsWith("netname")) {
					String[] keyValue = line.split(":\\s");
					ip.setNetname(keyValue[1].trim());
				}
				if (line.startsWith("descr")) {
					// System.out.println(ip.getInetnum());
					// System.out.println(line);
					String[] keyValue = line.split(":\\s");
					String descr = ip.getDescr();
					if (descr == null && keyValue.length == 2) {
						ip.setDescr(keyValue[1].trim());
					} else {
						StringBuffer sbDescr = new StringBuffer();
						sbDescr.append(descr).append(line);
					}
					
//					if (descr != null) {
//						for (String province : Constant.PROVINCES) {
//							if (descr.contains(province)) 
//								ip.setProvince(province);
//						}	
//					}
				}

				// sb.append(s1);
				// sb.append("\n");
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (br != null) {
					br.close();
				}
				if (reader != null) {
					reader.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return ipList;
	}

}
